import { Component, OnInit,ChangeDetectorRef,ViewChild } from '@angular/core';
import {MdPaginator, MdSnackBar, MD_DIALOG_DATA} from "@angular/material";
import {ApiServiceService} from '../api.service'
import {MdDialog, MdDialogRef} from '@angular/material';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {Observable} from 'rxjs/Observable';
import {DataSource} from '@angular/cdk/collections';


@Component({
  selector: 'app-acceptor-log',
  templateUrl: './acceptor-log.component.html',
  styleUrls: ['./acceptor-log.component.css']
})
export class AcceptorLogComponent implements OnInit {
  device
  logshow
  header = [
    {
      id: 'EBRID',
      name: 'ID'
    },
    {
      id: "EBRName",
      name: "名称"
    },
    {
      id: "URL",
      name: "URL地址"
    },
    {
      id: "status",
      name: "状态"
    },
    {
      id: "Longitude",
      name: "经度"
    },
    {
      id: "Latitude",
      name: "纬度"
    },
    {
      id: 'oper',
      name: '操作'
    }
  ];
  content = [
    { EBRID:'001',EBRName:'泥石流',URL:'www.baidu.com',status:'紧急',Longitude:'16',Latitude:'80'},
    { EBRID:'002',EBRName:'滑坡',URL:'www.qianyu.com',status:'一般',Longitude:'16.44',Latitude:'80'},
    { EBRID:'001',EBRName:'泥石流',URL:'www.baidu.com',status:'一般',Longitude:'16',Latitude:'80'},
    { EBRID:'002',EBRName:'滑坡',URL:'www.qianyu.com',status:'紧急',Longitude:'16.44',Latitude:'80'},
    { EBRID:'001',EBRName:'泥石流',URL:'www.baidu.com',status:'紧急',Longitude:'16',Latitude:'80'},
    { EBRID:'002',EBRName:'滑坡',URL:'www.qianyu.com',status:'紧急',Longitude:'16.44',Latitude:'80'},
    { EBRID:'001',EBRName:'泥石流',URL:'www.baidu.com',status:'不急',Longitude:'16',Latitude:'80'},
    { EBRID:'002',EBRName:'滑坡',URL:'www.qianyu.com',status:'紧急',Longitude:'16.44',Latitude:'80'},



  ];
  oper = [
    {
      icon: "report",
      title: "上报",
      do: this.report.bind(this)
    }
  ];


  constructor(private apiService:ApiServiceService,
              private changeDetectorRef:ChangeDetectorRef,
              public snackBar:MdSnackBar,
              public dialog:MdDialog) {
  }
  displayedColumns = [ /*'no',*/ 'EBRID', 'EBRName', 'URL', 'status', 'Longitude', 'Longitude','Latitude','operation'];
  soundSourceDatabase = new SoundSourceDatabase();
  dataSource:SoundSourceDataSource | null;
  allSelectedFlag:boolean;

  @ViewChild(MdPaginator) paginator:MdPaginator;

  ngOnInit() {
    this.dataSource = new SoundSourceDataSource(this.soundSourceDatabase, this.paginator);
    this.changeDetectorRef.detectChanges();
    this.allSelectedFlag = false;
    /*    this.apiService.initSoundSourceDatabase().subscribe(res=> {
     this.soundSourceDatabase.refreshData(res);
     })*/
    this.soundSourceDatabase.refreshData(this.content);
  }


  report(row) {
    let formData = new FormData();
    formData.append("EBMID", row.EBMID);
    this.apiService.reportInfo(formData)
      .subscribe(res=> {
        setTimeout(()=> {
          if (res.success) {
            this.openSnackBar("上报成功。", "");
          } else {
            this.openSnackBar("上报失败,请稍后再试。", "");
          }
        }, 3000)
      });
  }

  openSnackBar(message:string, action:string) {
    this.snackBar.open(message, action, {
      duration: 3000,
    });
  }

}



const COLORS = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple',
  'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray'];
const NAMES = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack',
  'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper',
  'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth'];

/*export interface SoundSourceData {
 deleteFlag:boolean;
 no:string;
 acaption:string;
 acontent:string;
 uid:string;
 isChecked:boolean,


 }*/
export interface SoundSourceData {

  // no:string;
  EBRID:string;
  EBRName:string;
  URL:string;
  status:string;
  Longitude:string;
  Latitude:string;
  operation?:any
  isChecked:boolean,
}

export class SoundSourceDatabase {
  /** Stream that emits whenever the data has been modified. */
  dataChange:BehaviorSubject<SoundSourceData[]> = new BehaviorSubject<SoundSourceData[]>([]);

  get data():SoundSourceData[] {
    return this.dataChange.value;
  }


  constructor() {
    // Fill up the database with 100 users.
    var _a:SoundSourceData[] = [
      // {
      //   "deleteFlag": false,
      //   "no": "1",
      //   "acaption": "网络节目源1",
      //   "acontent": "7001",
      //   "uid": "admin",
      //   "isChecked": false
      //
      // }
    ];
    this.dataChange.next(_a);


    // for (var i = 0; i < 100; i++) {
    //   this.addUser();
    // }
  }


  refreshData(array) {
    array.forEach(function (item, index) {
      item.no = index + 1;
    })
    this.dataChange.next(<SoundSourceData[]> array);
  }

  /** Adds a new user to the database. */
  addUser() {
    const copiedData = this.data.slice();
    copiedData.push(this.createNewUser());
    this.dataChange.next(copiedData);
  }

  /** Builds and returns a new User. */
  private createNewUser() {
    const name =
      NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' +
      NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.';

    return {

      no: (this.data.length + 1).toString(),
      EBRID: name,
      EBRName: Math.round(Math.random() * 100).toString(),
      URL: COLORS[Math.round(Math.random() * (COLORS.length - 1))],
      status: (this.data.length + 1).toString(),
      Longitude: name,
      Latitude: Math.round(Math.random() * 100).toString(),

      operation: (this.data.length + 1).toString(),
      isChecked: false,
    };
  }
}

export class SoundSourceDataSource extends DataSource<any> {
  constructor(private _soundSourceDatabase:SoundSourceDatabase, private _paginator:MdPaginator) {
    super();
  }

  /** Connect function called by the table to retrieve one stream containing the data to render. */
  connect():Observable<SoundSourceData[]> {
    const displayDataChanges = [
      this._soundSourceDatabase.dataChange,
      this._paginator.page,
    ];
    return Observable.merge(...displayDataChanges).map(() => {
      const data = this._soundSourceDatabase.data.slice();

      // Grab the page's slice of data.
      const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
      return data.splice(startIndex, this._paginator.pageSize);
    });

  }

  disconnect() {
  }
}
